﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Bon_It.Model;

namespace Bon.It.Server
{
    public partial class ContractManager
    {
        public Users[] GetUsers()
        {
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<Users>(u=>u.Groups);
            using (var context = GetDataContext(dlo))
            {
                 return context.Users.ToArray();
            }
        }

        public Users GetUserByUsername(string username)
        {
            throw new NotImplementedException();
        }

        public Users PersistUser(Users item)
        {
            using (var context = GetDataContext())
            {
                if (item.Id.Equals(0))
                {
                    context.Users.InsertOnSubmit(item);
                }
                else
                {
                    context.Users.Attach(item,true);
                }
                context.SubmitChanges();
                return item;
            }
        }

        public bool DeleteUser(Users item)
        {
            using (var context = GetDataContext())
            {
                Debug.Assert(item.Id.Equals(0), "the item is new... it can't be deleted");
                var foundItem = context.Users.FirstOrDefault(i => i.Id == item.Id);
                if (foundItem != null)
                {
                    context.Users.DeleteOnSubmit(foundItem);
                }
                context.SubmitChanges();
                return true;
            }
        }
    }
}
